Convert.ToString 方法 (System)

您所在的位置:网站首页 vb convert函数 Convert.ToString 方法 (System)

Convert.ToString 方法 (System)

2024-07-12 09:30| 来源: 网络整理| 查看: 265

Source:Convert.cs Source:Convert.cs Source:Convert.cs

使用指定的区域性特定格式设置信息将指定对象的值转换为其等效的字符串表示形式。

public: static System::String ^ ToString(System::Object ^ value, IFormatProvider ^ provider); public static string ToString (object value, IFormatProvider provider); public static string? ToString (object? value, IFormatProvider? provider); static member ToString : obj * IFormatProvider -> string Public Shared Function ToString (value As Object, provider As IFormatProvider) As String 参数 value Object

一个对象,用于提供要转换的值,或 null。

provider IFormatProvider

一个提供区域性特定的格式设置信息的对象。

返回 String

value 的字符串表示形式;如果 Empty 是一个值为 value 的对象,则为 null。 如果 value 为 null,则此方法返回 null。

示例

下面的示例定义了一个 Temperature 类,该类替代 Object.ToString 方法,但不实现 IConvertible 接口。 该示例演示了如何调用 Convert.ToString(Object, IFormatProvider) 方法,进而调用 Temperature.ToString 方法。

using System; public class Temperature { private decimal m_Temp; public Temperature(decimal temperature) { this.m_Temp = temperature; } public decimal Celsius { get { return this.m_Temp; } } public decimal Kelvin { get { return this.m_Temp + 273.15m; } } public decimal Fahrenheit { get { return Math.Round((decimal) (this.m_Temp * 9 / 5 + 32), 2); } } public override string ToString() { return m_Temp.ToString("N2") + " °C"; } } public class Example { public static void Main() { Temperature cold = new Temperature(-40); Temperature freezing = new Temperature(0); Temperature boiling = new Temperature(100); Console.WriteLine(Convert.ToString(cold, null)); Console.WriteLine(Convert.ToString(freezing, null)); Console.WriteLine(Convert.ToString(boiling, null)); } } // The example dosplays the following output: // -40.00 °C // 0.00 °C // 100.00 °C open System type Temperature(temperature: decimal) = member _.Celsius = temperature member _.Kelvin = temperature + 273.15m member _.Fahrenheit = Math.Round(temperature * 9m / 5m + 32m |> decimal, 2) override _.ToString() = temperature.ToString("N2") + " °C" let cold = Temperature -40 let freezing = Temperature 0 let boiling = Temperature 100 printfn $"{Convert.ToString(cold, null)}" printfn $"{Convert.ToString(freezing, null)}" printfn $"{Convert.ToString(boiling, null)}" // The example dosplays the following output: // -40.00 °C // 0.00 °C // 100.00 °C Public Class Temperature Private m_Temp As Decimal Public Sub New(temperature As Decimal) Me.m_Temp = temperature End Sub Public ReadOnly Property Celsius() As Decimal Get Return Me.m_Temp End Get End Property Public ReadOnly Property Kelvin() As Decimal Get Return Me.m_Temp + 273.15d End Get End Property Public ReadOnly Property Fahrenheit() As Decimal Get Return Math.Round(CDec(Me.m_Temp * 9 / 5 + 32), 2) End Get End Property Public Overrides Function ToString() As String Return m_Temp.ToString("N2") & " °C" End Function End Class Module Example Public Sub Main() Dim cold As New Temperature(-40) Dim freezing As New Temperature(0) Dim boiling As New Temperature(100) Console.WriteLine(Convert.ToString(cold, Nothing)) Console.WriteLine(Convert.ToString(freezing, Nothing)) Console.WriteLine(Convert.ToString(boiling, Nothing)) End Sub End Module ' The example displays the following output: ' -40.00 °C ' 0.00 °C ' 100.00 °C

下面的示例定义了一个 Temperature 实现 IFormattable 接口但不实现 接口的 IConvertible 类。 其 IFormattable.ToString 实现表示 Temperature 以摄氏度、华氏度或开氏度为单位的值,具体取决于格式字符串。 该示例还定义了一个TemperatureProvider类,该类实现IFormatProvider并提供由 类的Temperature实现使用的IFormattable随机生成的格式字符串。

using System; public class Temperature : IFormattable { private decimal m_Temp; public Temperature(decimal temperature) { this.m_Temp = temperature; } public decimal Celsius { get { return this.m_Temp; } } public decimal Kelvin { get { return this.m_Temp + 273.15m; } } public decimal Fahrenheit { get { return Math.Round(this.m_Temp * 9m / 5m + 32m, 2); } } public override String ToString() { return ToString("G", null); } public String ToString(String fmt, IFormatProvider provider) { TemperatureProvider formatter = null; if (provider != null) formatter = provider.GetFormat(typeof(TemperatureProvider)) as TemperatureProvider; if (String.IsNullOrWhiteSpace(fmt)) { if (formatter != null) fmt = formatter.Format; else fmt = "G"; } switch (fmt.ToUpper()) { case "G": case "C": return m_Temp.ToString("N2") + " °C"; case "F": return Fahrenheit.ToString("N2") + " °F"; case "K": return Kelvin.ToString("N2") + " K"; default: throw new FormatException(String.Format("'{0}' is not a valid format specifier.", fmt)); } } } public class TemperatureProvider : IFormatProvider { private String[] fmtStrings = { "C", "G", "F", "K" }; private Random rnd = new Random(); public Object GetFormat(Type formatType) { return this; } public String Format { get { return fmtStrings[rnd.Next(0, fmtStrings.Length)]; } } } public class Example { public static void Main() { Temperature cold = new Temperature (-40); Temperature freezing = new Temperature (0); Temperature boiling = new Temperature (100); TemperatureProvider tp = new TemperatureProvider(); Console.WriteLine(Convert.ToString(cold, tp)); Console.WriteLine(Convert.ToString(freezing, tp)); Console.WriteLine(Convert.ToString(boiling, tp)); } } // The example displays output like the following: // -40.00 °C // 273.15 K // 100.00 °C open System [] type TemperatureProvider() = let fmtStrings = [| "C"; "G"; "F"; "K" |] let rnd = Random() member _.Format = fmtStrings[rnd.Next(0, fmtStrings.Length)] interface IFormatProvider with member this.GetFormat(formatType: Type) = this type Temperature(temperature: decimal) = member _.Celsius = temperature member _.Kelvin = temperature + 273.15m member _.Fahrenheit = Math.Round(temperature * 9m / 5m + 32m, 2) override this.ToString() = this.ToString("G", null) member this.ToString(fmt: string, provider: IFormatProvider) = let formatter = match provider with | null -> null | _ -> match provider.GetFormat typeof with | :? TemperatureProvider as x -> x | _ -> null let fmt = if String.IsNullOrWhiteSpace fmt then if formatter null then formatter.Format else "G" else fmt match fmt.ToUpper() with | "G" | "C" -> $"{temperature:N2} °C" | "F" -> $"{this.Fahrenheit:N2} °F" | "K" -> $"{this.Kelvin:N2} K" | _ -> raise (FormatException $"'{fmt}' is not a valid format specifier.") let cold = Temperature -40 let freezing = Temperature 0 let boiling = Temperature 100 let tp = TemperatureProvider() printfn $"{Convert.ToString(cold, tp)}" printfn $"{Convert.ToString(freezing, tp)}" printfn $"{Convert.ToString(boiling, tp)}" // The example displays output like the following: // -40.00 °C // 273.15 K // 100.00 °C Public Class Temperature : Implements IFormattable Private m_Temp As Decimal Public Sub New(temperature As Decimal) Me.m_Temp = temperature End Sub Public ReadOnly Property Celsius As Decimal Get Return Me.m_Temp End Get End Property Public ReadOnly Property Kelvin As Decimal Get Return Me.m_Temp + 273.15d End Get End Property Public ReadOnly Property Fahrenheit As Decimal Get Return Math.Round(CDec(Me.m_Temp * 9 / 5 + 32), 2) End Get End Property Public Overrides Function ToString() As String Return ToString("G", Nothing) End Function Public Overloads Function ToString(fmt As String, provider As IFormatProvider) As String _ Implements IFormattable.ToString Dim formatter As TemperatureProvider = Nothing If provider IsNot Nothing Then formatter = TryCast(provider.GetFormat(GetType(TemperatureProvider)), TemperatureProvider) If String.IsNullOrWhiteSpace(fmt) Then If formatter IsNot Nothing Then fmt = formatter.Format Else fmt = "G" End If End If Select Case fmt.ToUpper() Case "G", "C" Return m_Temp.ToString("N2") & " °C" Case "F" Return Fahrenheit.ToString("N2") + " °F" Case "K" Return Kelvin.ToString("N2") + " K" Case Else Throw New FormatException(String.Format("'{0}' is not a valid format specifier.", fmt)) End Select End Function End Class Public Class TemperatureProvider : Implements IFormatProvider Private fmtStrings() As String = { "C", "G", "F", "K" } Private rnd As New Random() Public Function GetFormat(formatType As Type) As Object _ Implements IFormatProvider.GetFormat Return Me End Function Public ReadOnly Property Format As String Get Return fmtStrings(rnd.Next(0, fmtStrings.Length)) End Get End Property End Class Module Example Public Sub Main() Dim cold As New Temperature(-40) Dim freezing As New Temperature(0) Dim boiling As New Temperature(100) Dim tp As New TemperatureProvider() Console.WriteLine(Convert.ToString(cold, tp)) Console.WriteLine(Convert.ToString(freezing, tp)) Console.WriteLine(Convert.ToString(boiling, tp)) End Sub End Module ' The example displays output like the following: ' -40.00 °C ' 273.15 K ' 100.00 °C 注解

value如果 参数实现 IConvertible 接口,该方法将调用 IConvertible.ToString(IFormatProvider) 的value实现。 否则,如果 value 参数实现 IFormattable 接口,该方法将调用其 IFormattable.ToString(String, IFormatProvider) 实现。 如果 value 两个接口均未实现,则该方法调用 value 参数的 ToString() 方法,并忽略 参数 provider 。

provider如果 value 参数实现 或 IFormattable 接口,IConvertible则使用 参数。 参数的 provider 最常见用途是指定 转换 value中使用的特定于区域性的信息。 例如,如果 value 参数是负十进制数,则 provider 参数可以提供有关用于负号和小数分隔符的表示法的区域性特定信息。 下一部分中的第二个示例演示了不提供区分区域性的格式设置信息的格式提供程序。

适用于


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3